<template>
  <div class="alipay-config gray-box top-redius border-bottom-gray">
    <div class="title">{{ $t("sysManagement.sensitiveConfiguration") }}</div>
    <el-row>
      <el-col :span="11">
        <el-form
          @submit.native.prevent
          ref="dataForm"
          label-width="5px"
          size="mini"
          class="set-form"
          label-position="left"
          @keyup.enter.native="dataFormSubmit()"
          :rules="dataRule"
          :model="dataForm"
        >
          <el-form-item prop="sensitiveWord">
            <el-alert
              :title="$t('sysManagement.sensitiveAlert')"
              type="warning"
              :closable="false"
              show-icon
            />
            <el-input
              v-model="dataForm.sensitiveWord"
              type="textarea"
              rows="28"
              :placeholder="$t('sysManagement.sensitiveWord')"
              controls-position="right"
              @input="inputLimit"
            ></el-input>
          </el-form-item>
        </el-form>
      </el-col>
      <el-col :span="11" :offset="2">
        <el-form
          @submit.native.prevent
          ref="dataForm"
          label-width="5px"
          size="mini"
          class="set-form"
          label-position="left"
          @keyup.enter.native="dataFormSubmit()"
          :rules="dataRule"
          :model="dataFormByEmoji"
        >
          <el-form-item prop="sensitiveWord">
            <el-alert
              :title="$t('sysManagement.sensitiveAlertByEmoji')"
              type="warning"
              :closable="false"
              show-icon
            />
            <el-input
              v-model="dataFormByEmoji.sensitiveSymbol"
              @input="valueToFilterByEmoji"
              type="textarea"
              rows="28"
              :placeholder="$t('sysManagement.sensitiveAlertByEmoji')"
              controls-position="right"
            ></el-input>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
    <span slot="footer" class="dialog-footer">
      <div></div>
      <div class="default-btn" @click="dataFormSubmit()">
        {{ $t("sysManagement.save") }}
      </div>
      <!-- <el-button @click="visible = false">取消</el-button> -->
    </span>
  </div>
</template>

<script>
export default {
  data() {
    return {
      dataForm: {
        sensitiveWord: null,
      },
      dataFormByEmoji: {
        sensitiveSymbol: null,
      },
      dataRule: {},
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    valueToFilterByEmoji() {
      let userInput = this.dataFormByEmoji.sensitiveSymbol;
      function sanitizeInput(input) {
        const regex = /[^\p{P}\p{S}]+/gu;
        let res = input.replace(regex, "");
        const emojiRegex = /[\uD83C-\uDBFF\uDC00-\uDFFF]+/g;
        // 使用空字符串替换匹配到的表情符号
        let filteredText = res.replace(emojiRegex, "");
        return filteredText;
      }
      // 示例用法
      const sanitizedInput = sanitizeInput(userInput);
      this.dataFormByEmoji.sensitiveSymbol = sanitizedInput;
    },
    init() {
      // this.$refs['dataForm'].resetFields()
      this.$nextTick(() => {
        this.$http({
          url: this.$http.adornUrl("/sys/pconfig/info/" + "SENSITIVE_WORDS"),
          method: "get",
          params: this.$http.adornParams(),
        }).then(({ data }) => {
          if (data) {
            this.dataForm = JSON.parse(data);
          }
        });

        this.$http({
          url: this.$http.adornUrl("/sys/pconfig/info/" + "SENSITIVE_SYMBOLS"),
          method: "get",
          params: this.$http.adornParams(),
        }).then(({ data }) => {
          if (data) {
            this.dataFormByEmoji = JSON.parse(data);
          }
        });
        this.$refs["dataForm"].resetFields();
      });
    },
    inputLimit(val) {
      // 正则匹配非中英文及数字以及中文逗号的字符
      const reg = /[^\u4e00-\u9fa5\uff0c]/;
      // 查找是否有非中英文及数字的字符
      // 没有则返回-1，有则返回对应位置
      const n = val.search(reg);
      if (
        n !== -1 ||
        (val[val.length - 2] === "，" && val[val.length - 1] === "，")
      ) {
        this.dataForm.sensitiveWord = val.slice(0, n);
        this.$forceUpdate();
      }
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          Promise.all([
            this.$http({
              url: this.$http.adornUrl("/sys/pconfig/save"),
              method: "post",
              data: this.$http.adornData({
                paramKey: "SENSITIVE_WORDS",
                paramValue: JSON.stringify(this.dataForm),
                remark: this.$i18n.t("sysManagement.sensitiveConfiguration"),
              }),
            }),
            this.$http({
              url: this.$http.adornUrl("/sys/pconfig/save"),
              method: "post",
              data: this.$http.adornData({
                paramKey: "SENSITIVE_SYMBOLS",
                paramValue: JSON.stringify(this.dataFormByEmoji),
                remark: this.$i18n.t(
                  "sysManagement.sensitiveConfigurationByEmoji"
                ),
              }),
            }),
          ]).then((res) => {
            console.log("res", res);
            if (res[0].code == "00000" || res[1].code == "00000") {
              this.$message({
                message: this.$i18n.t("publics.operation"),
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            }
          });
        }
      });
    },
    /**
     * dataform -> alipay
     */
    // getAlipayParam () {
    //   this.alipay.appId = this.dataForm.appId
    //   this.alipay.appCertPath = this.dataForm.appCertPath
    //   this.alipay.alipayCertPath = this.dataForm.alipayCertPath
    //   this.alipay.alipayRootCertPath = this.dataForm.alipayRootCertPath
    //   this.alipay.appPrivateKey = this.dataForm.appPrivateKey
    // },
    /**
     *  alipay -> dataform
     */
    // getDataParam (data) {
    //   this.dataForm.id = data.id
    //   this.dataForm.paramKey = data.paramKey
    //   this.dataForm.paramValue = data.paramValue
    //   this.dataForm.appId = this.alipay.appId
    //   this.dataForm.appCertPath = this.alipay.appCertPath
    //   this.dataForm.alipayCertPath = this.alipay.alipayCertPath
    //   this.dataForm.alipayRootCertPath = this.alipay.alipayRootCertPath
    //   this.dataForm.appPrivateKey = this.alipay.appPrivateKey
    // }
  },
};
</script>
<style scoped>
.dialog-footer {
  display: flex;
  justify-content: space-between;
  box-sizing: border-box;
  padding: 0 20px;
}
</style>
